Conceptual Asides
Execution Context (Global)β
Main Components:
- Global Object
- Global = Not Inside a Function
this
- whoever is running the application.- in browser it is a
window
object - in other execution context,
this
would be different
- in browser it is a
- Outer Environment
- Your Code
Hoistingβ
b();
console.log(a);
var a = 'Hello World';
function b(){
console.log('Called b!')
}
OUTPUTS:
undefined
Called b!
function b
- works because on the initial load of JS, they pass through the code and function b is allocated with a memory space.- a is allocated a space as well, but currently is undefined, only when the code actually runs is when
Hello World
is assigned toa
- This is called hoisting, JS engine 'hoists' the functions and variable declarations to the top of the code.
ES6β
- ECMAScript - Next version of JS
Synchronousβ
Typingβ
- No such thing as Static Typing in JS by default, everything is Dynamic Typed.
- Everything is figured out on the fly
Coercionβ
- Since vars are dynamically typed, when you try to add different types, JS converts to a specific types
Strict Equalityβ
===
- do not do any coercion/typing when comparing values
Object
can contains:
- Primitive Property
- another Object Property
- Function Method
Object Propertyβ
Dynamic Property access:β
var person = new Object();
person["firstname"] = "Tony";
can be access:
var property = "firstname";
console.log(person[property]);
Dotβ
console.log(person.firstname);
- We can nest
person.address = new Object();
person.address.street = "111 main"
Literalsβ
- instead of new Object
var person = {};
- They are the same under the hood.
- so we can shorthand it declare:
var person = { firstname: 'Tony'};
SAME AS:
var person = new Object();
person.firstname = 'tony'
SAME AS:
var person = new Object();
person["firstname"]= "tony";
JSON vs Object Literalsβ
- JSON is a more strict rules
- Object literals are not qouted
- they are not the same
**Convert Object Literal to JSON
var objectLiteral = {
firstName: 'Mary'
}
JSON.stringify(objectLiteral)
JSON String to Object Literal
var object = JSON.parse ('{ "firstname": "Mary"}')
Higher Order Functionsβ
- Functions are Objects
- You can add:
- Primitive
- Object
- Function
- Name (optional)
- Code (Invocable)
function greet(){
console.log('hi');
}
greet.language = 'English';
Function statement vs expressionβ
Statement:
funcion greet(){
console.log('hi');
}
Expression (non-hoisted
- Anonymous function assigned to variable
var anon = function(){
console.log('hi');
}
anon();
Pass it around:
function log (a){
a();
}
log(()=>{
console.log('helo');
})
Thisβ
- Dynamically changes based on where it is attached:
Method THISβ
PITFALL by default this
refers to the global execution context:
var a = function(){
this.newVariable ='global'
}
a();
console.log(newVariable);
Object THISβ
var c = {
name: 'c object',
log: ()=>{
console.log(this)
}
}
c.log(); // 'c object'
Mutate (object)β
var person = {
name: "mel",
changeName: function() {
this.name = "april"
},
getName: function() {
console.log(this.name);
}
}
person.changeName();
console.log(person.getName());
Selfβ
PITFALL : nested this calls goes back to refering to the global reference of this:
var person = {
name: "mel",
changeName: function() {
this.name = "april"
var setname = function(newname){
this.name = newname; //GLOBAL this
}
setname("bug")
},
getName: function() {
console.log(this.name);
}
}
To work around this:
changeName: function() {
var self = this;
self.name = "april"
var setname = function(newname){
self.name = newname; //GLOBAL this
}
setname("bug")
}